#include <head.h>

int main(int argc, char const *argv[])
{
    char buf[128];
    char buf1[128];
    int i; // 用户购买数量
    int j; // 库存量

    int fd1 = open("ke.txt", O_CREAT | O_RDWR, 0666);
    read(fd1, buf, sizeof(buf));
    close(fd1);
int fd = open("ke.txt", O_CREAT | O_RDWR|O_TRUNC, 0666);
    flock(fd, LOCK_EX);
    printf("库存：");
    for (int k = 0; k < strlen(buf); k++)
    {
        printf("%c", buf[i]);
        
        if (k%10 == 0)
        {
            printf("\n");
        }
    }
    lseek(fd, 0, SEEK_SET);
    printf("请输入您所需的货物数量：");
    scanf("%d", &i);
    if (strlen(buf)<i)
    {
        printf("库存不足！\n");
        return 0;
    }
    for (int k = 0; k < strlen(buf)-i; k++)
    {
       buf1[k]=buf[k];
    }
    write(fd, buf1, strlen(buf1));
    flock(fd, LOCK_UN);
    close(fd);
    printf("您的购买成功！\n");
    return 0;
}
